/**
 * @author trex279
 */

thisApplication.sessions.redditLogin = function(){
	var childInterface = document.getElementById("redditPage").contentWindow.childSandboxBridge;
	var userName = $('#userName').val();
	userName_g = userName;
	var password = $('#password').val();
	childInterface.redditLogin(userName, password);
	//return 1;
}

thisApplication.sessions.redditLogout = function(){
	var childInterface = document.getElementById("redditPage").contentWindow.childSandboxBridge;
	childInterface.redditLogout();
}

var parentsExposedObject = {}; 
parentsExposedObject.loginResult = function(result, userName){
	if(result=="true"){
		userName_g = userName;
		isLoggedin = true;
		$('#loginError').css('display','none');
		$('#loggedIn').html("You have been logged in as "+userName+".").css('display','inline');
		$('#logout').css('display','inline');
		$('#redditLogin').css('display','none');
		$('#logout').click(function(event){
			event.preventDefault();
			thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
			thisApplication.sessions.redditLogout();
		});
	}
	else if(result=="false"){
		isLoggedin = false;
		$('#redditLogin').css('display','inline');
		$('#loggedIn').css('display','none');
		$('#logout').css('display','none');
	}
	else if(result=="error"){
		isLoggedin = false;
		$('#loginError').css('display','inline');
		$('#redditLogin').css('display','inline');
		$('#loggedIn').css('display','none');
		$('#logout').css('display','none');
	}
	thisApplication.nativeWin.dispatchEvent(loadingCompleteEvent);
}

parentsExposedObject.showLoginform = function(){
	$('#redditLogin').css('display','inline');
}


function redditLogin(){ //for use with html
	thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
	thisApplication.sessions.redditLogin();
}
